<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/20
 * Time: 9:30
 */
namespace app\business\controller;

use think\Db;
use think\Session;

class Goods extends Common
{
	/**
	 * 判断是否登录
	*/
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 查看所有商品
	 * @param string get.title 要检索的字段
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('product')
			->where('bid',Session::get('business.id'))
			->whereIn('status',[1,6,7]);
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('pname|barcode','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->order('id desc')->paginate(8,false,$where);
		
		return view('index',compact('result','title'));
	}
	
	/**
	 * 查看视频
	 * @param string get.id  商品id
	*/
	public function previewvideo()
	{
		$id = input('get.id');
		$video = Db::table('product')->where('id',$id)->value('video');
		return view('preview',compact('video'));
	}
	
	/**
	 * 删除商品
	 * @param string post.id 商品id
	*/
	public function goodsdel()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('product')->where('id',$id)->update(['status'=>2]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 查看图片
	 * @param string post.id 商品id
	*/
	public function previewimage()
	{
		$id = input('get.id');
		$result = Db::table('product')->where('id',$id)->find();
		$result['image'] = json_decode($result['image']);
		return view('image',compact('result'));
	}
	
	/**
	 * 审核的商品
	 * @param string get.title 要检索的字段
	*/
	public function examine()
	{
		$where = ['query'=>[]];
		$db = Db::table('product')
			->where('bid',Session::get('business.id'))
			->whereIn('status',[3,4,5]);
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('pname|barcode','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->order('id desc')->paginate(8,false,$where);
		return view('examine',compact('result','title'));
	}
	
	/**
	 * 描述
	 * @param string get.id 商品id
	*/
	public function introduct()
	{
		$id = input('get.id');
		$introduce = Db::table('product')->where('id',$id)->value('introduce');
		return view('introduct',compact('introduce'));
	}
	
}